﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public delegate void MouseMoveSecondHandler();
    public delegate void MouseUpSecondHandler();

    public partial class DragAndDropForm : Form
    {
        public DragAndDropForm()
        {
            InitializeComponent();
        }

        public static event MouseMoveSecondHandler MouseMoveChanged;
        public static event MouseUpSecondHandler MouseUpChanged;

        public int offsetX = 0;
        public int offsetY = 0;
        public object value = null;

        public static int rootX;
        public static int rootY;

        public static List<DragAndDropForm> dragAndDropForms = new List<DragAndDropForm>();
        public static DragAndDropForm mainDragAndDropForm = null;


        public DragAndDropForm(int x, int y, object value)
        {
            InitializeComponent();

            offsetX = x;
            offsetY = y;
            this.value = value;

            labelValue.Text = value.ToString();
        }

        private void form_MouseMove(object sender, MouseEventArgs e)
        {
            MouseMoveExtra(e);
        }

        private void MouseMoveExtra(MouseEventArgs e)
        {
            Cursor = Cursors.Hand;
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                    SetPositions();
            }
            else
            {
                ClearForms();
            }

            if (MouseMoveChanged != null)
                MouseMoveChanged();
        }

        public static void SetPositions()
        {
            if (mainDragAndDropForm != null)
            {
                mainDragAndDropForm.Location = new Point(Cursor.Position.X - mainDragAndDropForm.Width / 2, Cursor.Position.Y - mainDragAndDropForm.Height / 2);

                foreach (var f in dragAndDropForms)
                {
                    f.Location = new Point(Cursor.Position.X - mainDragAndDropForm.Width / 2 + mainDragAndDropForm.Width * f.offsetX, Cursor.Position.Y - mainDragAndDropForm.Height / 2 + mainDragAndDropForm.Height * f.offsetY);
                }
            }
        }

        internal static void SowsForms()
        {
            if (mainDragAndDropForm != null)
            {

                mainDragAndDropForm.Show();
                foreach (var f in dragAndDropForms)
                    f.Show();

                SetPositions();

                mainDragAndDropForm.Opacity = 0.7;
                mainDragAndDropForm.Refresh();
                foreach (var f in dragAndDropForms)
                {
                    f.Opacity = 0.7;
                    f.Refresh();
                }
            }
        }

        internal static void setFormsSize()
        {
            if (mainDragAndDropForm != null)
            {
                foreach (var f in dragAndDropForms)
                {
                    f.Height = mainDragAndDropForm.Height;
                    f.Width = mainDragAndDropForm.Width;

                    f.MaximumSize = mainDragAndDropForm.MaximumSize;
                    f.MinimumSize = mainDragAndDropForm.MinimumSize;
                }
            }
        }

        public static void ClearForms()
        {
            if (mainDragAndDropForm != null)
            {
                mainDragAndDropForm.Close();
                mainDragAndDropForm = null;

                foreach (var f in dragAndDropForms)
                {
                    f.Close();
                }

                dragAndDropForms.Clear();
            }
        }

        public static void setBackground(Color color)
        {
            if (mainDragAndDropForm != null)
            {
                mainDragAndDropForm.BackColor = color;
                foreach (var f in dragAndDropForms)
                {
                    f.BackColor = color;
                }
            }
        }

        private void form_MouseUp(object sender, MouseEventArgs e)
        {
            MouseUpExtra();
        }

        private void MouseUpExtra()
        {
            if (MouseUpChanged != null)
                MouseUpChanged();

            Cursor = Cursors.Default;

        }

    }
}
